<!--
 * @Author: your name
 * @Date: 2021-02-23 10:39:10
 * @LastEditTime: 2021-02-25 17:48:55
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \mypro\table.html
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            font-family: '楷体';
        }

        main {
            width: 600px;
            height: 400px;
            background-color: #f9f1db;
            margin: 100px auto;
        }

        ul {
            width: 100%;
        }

        li {
            list-style-type: none;
            float: left;
            /* width: 20%; */
            height: 50px;
            text-align: center;
            line-height: 50px;
            background-color: skyblue;
            color: #f9f1db;
            cursor: pointer;
            font-size: 20px;
        }

        div {
            width: 100%;
            height: 350px;
            background-color: skyblue;
            float: left;
            font-size: 40px;
            line-height: 350px;
            text-align: center;
            color: #f9f1db;
            display: none;
        }

        .active {
            color: skyblue;
            background-color: #f9f1db;
        }

        .con {
            color: skyblue;
            background-color: #f9f1db;
            display: block;
        }
    </style>
</head>

<body>
    <main>
        <!-- <ul>
            <li class="active">腾讯</li>
            <li>阿里</li>
            <li>百度</li>
            <li>新浪</li>
        </ul>
        <div class="con">腾讯</div>
        <div>阿里</div>
        <div>百度</div>
        <div>新浪</div> -->

        <ul>
            <li class="active">腾讯</li>
            <li>阿里</li>
            <li>百度</li>
            <li>新浪</li>
            <li>京东</li>
        </ul>
        <div class="con">腾讯</div>
        <div>阿里</div>
        <div>百度</div>
        <div>新浪</div>
        <div>京东</div>
    </main>
</body>
<script src="./tool.js"></script>
<script>
    // 构造函数
    function Fn() {
        //console.log(this) 指向实例化对象box
        this.lis = document.querySelectorAll('li');
        this.vs = document.querySelectorAll('div');
        this.yyk()
    }
    // 构造函数的原型
    Fn.prototype.yyk = function () {
        //console.log(this.lis)
        let tab = this.lis;
        let con = this.vs;
        for (let i = 0; i < tab.length; i++) {
            this.lis[i].onclick = function () {
                for (let j = 0; j < tab.length; j++) {
                    tab[j].classList.remove('active');
                    con[j].classList.remove('con');
                }
                tab[i].classList.add('active');
                con[i].classList.add('con');
            }
        }
    }
    // 实例化对象
    let box = new Fn();
    // console.log(box)

    // 动态设置li宽度
    // 获取ul
    let ul = document.querySelector('ul');
    // li的宽度 = ul的宽度 / li的个数 => getCss(ul, 'width') / box.lis.length
    let liWidth = parseInt(getCss(ul, 'width')) / box.lis.length;
    //console.log(getCss(ul, 'width'))
    //console.log(box.lis.length)
    // 遍历li设置宽度
    for(let i=0; i<box.lis.length; i++){
        getCss(box.lis[i], 'width', liWidth + 'px')
    }
</script>

</html>